package com.singfan.core.dao.mongo.impl;

import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
import com.singfan.common.nosql.mongo.MongoDaoBase;
import com.singfan.core.dao.mongo.IBarberLocationDao;
import org.bson.Document;
import org.bson.types.ObjectId;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

/**
 * Author: hansonghe
 * Date: 2015-08-21 10:53
 * Desc:
 */
@Repository
public class BarberLocationDaoImpl extends MongoDaoBase implements IBarberLocationDao {
    private static final String COLLECTION_NAME = "barber";
    @Override
    public void createBarberLocation(long barberId, int cityId, int businessCircleId, List<Integer> serviceTypeList, double longitude, double latitude, int position) {
//        Document barberDocument = new Document();
//        barberDocument.put("_id", new ObjectId());
//        barberDocument.put("barber_id", barberId);
//        barberDocument.put("location", new Double[]{longitude,latitude});
//        insertOne("barber", barberDocument);

        DBObject dbo = new BasicDBObject();
        dbo.put("_id", new ObjectId());
        dbo.put("barber_id", barberId);
        dbo.put("city_id", cityId);
        dbo.put("business_circle_id", businessCircleId);
        Integer[] serviceTypeArr = new Integer[serviceTypeList.size()];
        for (int i = 0; i<serviceTypeList.size(); i++){
            serviceTypeArr[i] = serviceTypeList.get(i);
        }
        dbo.put("service_type", serviceTypeArr);
        dbo.put("position", position);
        dbo.put("location", new double[]{longitude,latitude});
        insertObj(COLLECTION_NAME, dbo);
    }

    @Override
    public List<Long> findBarberNearbyLocation(int serviceType, int cityId, int businessCircleId, double longitude, double latitude, int position, int pageIndex, int pageSize) {
//        Document searchDoc = new Document();
//        if (serviceType > 0){
//            searchDoc.put("service_type", serviceType);
//        }
//        if (businessCircleId == 0){
//            searchDoc.put("city_id", cityId);
//        }
//        else {
//            searchDoc.put("business_circle_id", businessCircleId);
//        }
//        searchDoc.put("location", new Document("$near", new Double[]{longitude, latitude}));
//        List<Document> barberDocList = selectList(COLLECTION_NAME, searchDoc, pageIndex, pageSize);
//        List<Long> barberIdList = new ArrayList<>(barberDocList.size());
//        for (Document barberDoc : barberDocList){
//            barberIdList.add(barberDoc.getLong("barber_id"));
//        }
//
//        return barberIdList;

        BasicDBObject searchObj = new BasicDBObject();
        if (serviceType > 0){
            searchObj.put("service_type", serviceType);
        }
        if (position > 0){
            searchObj.put("position", position);
        }
        if (businessCircleId == 0){
            searchObj.put("city_id", cityId);
        }
        else {
            searchObj.put("business_circle_id", businessCircleId);
        }
        searchObj.put("location", new BasicDBObject("$near", new double[]{longitude, latitude}));
        List<DBObject> barberObjList = selectList(COLLECTION_NAME, searchObj, pageIndex, pageSize);
        List<Long> barberIdList = new ArrayList<>(barberObjList.size());
        for (DBObject barberObj : barberObjList){
            barberIdList.add(Long.valueOf(barberObj.get("barber_id").toString()));
        }

        return barberIdList;
    }

    @Override
    protected String getMongoDatabaseName() {
        return "singfan_location_0";
    }
}